import cookesTools from '@/mytools/cookesTools'
import axios from 'axios'
import router from '@/router'
import {
  MessageBox,
  Message
} from 'element-ui'

const service = axios.create({
  baseURL: "http://localhost:20932/api/",
  timeout: 20000
})
service.interceptors.response.use(
  response => {
    if (response.status == 200) {
      const res = response.data
      if (res.code !== 1) {
        if(res.code==50008)
        {
          MessageBox.confirm('请先登录', {
            confirmButtonText: '跳转登录页',
            cancelButtonText: 'Cancel',
            type: 'warning'
          }).then(() => {
            router.push("/");
          })
        }
        else{
          Message({
            message: res.message || 'Error',
            type: 'error',
            duration: 5 * 1000
          })
        }
        alert(res.message)
        return Promise.reject(new Error(res.message || 'Error'))
      } else {
        return res
      }
    } else {
      Message({
        message: "请求接口出错,请确它是否可以正常访问",
        type: 'error',
        duration: 5 * 1000
      })
    }
  }
)

service.interceptors.request.use(
  config => {
    let token=cookesTools.getToken();
    if (token) {
      config.headers['X-Token'] = token;
    }
    return config
  }
)

export default service
